package com.ruoyi.production.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.OperationStepsMapper;
import com.ruoyi.production.domain.OperationSteps;
import com.ruoyi.production.service.IOperationStepsService;

/**
 * 工序设置Service业务层处理
 * 
 * @author admin
 * @date 2024-08-02
 */
@Service
public class OperationStepsServiceImpl implements IOperationStepsService 
{
    @Autowired
    private OperationStepsMapper operationStepsMapper;
    @Autowired
    private SqlSessionFactory sqlSessionFactory;


    /**
     * 查询工序设置
     * 
     * @param stepsId 工序设置主键
     * @return 工序设置
     */
    @Override
    public OperationSteps selectOperationStepsByStepsId(Long stepsId)
    {
        return operationStepsMapper.selectOperationStepsByStepsId(stepsId);
    }

    /**
     * 查询工序设置列表
     * 
     * @param operationSteps 工序设置
     * @return 工序设置
     */
    @Override
    public List<OperationSteps> selectOperationStepsList(OperationSteps operationSteps)
    {
        return operationStepsMapper.selectOperationStepsList(operationSteps);
    }

    /**
     * 新增工序设置
     * 
     * @param operationSteps 工序设置
     * @return 结果
     */
    @Override
    public int insertOperationSteps(OperationSteps operationSteps)
    {
        operationSteps.setCreateTime(DateUtils.getNowDate());
        return operationStepsMapper.insertOperationSteps(operationSteps);
    }


    /**
     * 批量新增工序设置
     *
     * @param operationStepss 工序设置List
     * @return 结果
     */
    @Override
    public int batchInsertOperationSteps(List<OperationSteps> operationStepss)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(operationStepss)) {
            try {
                for (int i = 0; i < operationStepss.size(); i++) {
                    int row = operationStepsMapper.insertOperationSteps(operationStepss.get(i));
                    // 防止内存溢出，没100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%3 == 0) || i == operationStepss.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }


    /**
     * 修改工序设置
     * 
     * @param operationSteps 工序设置
     * @return 结果
     */
    @Override
    public int updateOperationSteps(OperationSteps operationSteps)
    {
        return operationStepsMapper.updateOperationSteps(operationSteps);
    }

    /**
     * 批量删除工序设置
     * 
     * @param stepsIds 需要删除的工序设置主键
     * @return 结果
     */
    @Override
    public int deleteOperationStepsByStepsIds(Long[] stepsIds)
    {
        return operationStepsMapper.deleteOperationStepsByStepsIds(stepsIds);
    }

    /**
     * 删除工序设置信息
     * 
     * @param stepsId 工序设置主键
     * @return 结果
     */
    @Override
    public int deleteOperationStepsByStepsId(Long stepsId)
    {
        return operationStepsMapper.deleteOperationStepsByStepsId(stepsId);
    }
}
